export default class DateUtil {
    /**
     * 获取n年前的日期
     * @param yearNum
     * @param date
     * @returns
     */
    static getLastYear(yearNum: number, date: Date = new Date()): Date {
        date.setFullYear(date.getFullYear() - yearNum)
        return date;
    }
    /* 
      获取前几个月，默认前一个月的今天(传入负数表示下几个月)
    */
    static getLastMonth(monthNum: number, date: Date = new Date()): Date {
        date.setMonth(date.getMonth() - monthNum)
        return date;
    }
    /**
     * 获取N天以前的时间
     */
    static getLastDay(dayNum: number, date: Date = new Date()): Date {
        let today = date ? date : new Date()
        return new Date(today.getTime() - (dayNum * 24 * 60 * 60 * 1000))
    }
    /**
     * 上午下午晚上
     * @param date 
     * @returns 
     */
    static amOrFm(date: Date): string {
        if (date.getHours() >= 0 && date.getHours() < 12) {
            return "上午"
        } else if (date.getHours() >= 12 && date.getHours() < 18) {
            return "下午"
        } else {
            return "晚上"
        }
    }

    /**
     * 生日转年龄
     * @param {Object} birthDay
     */
    static birthDayToAge(birthday: Date): number {
        var ageDifMs = Date.now() - new Date(birthday).getTime();
        var ageDate = new Date(ageDifMs);
        return Math.abs(ageDate.getUTCFullYear() - 1970);
    }
    /**
    * 获取是星期几
    * @param {Object} dateStr
    */
    static getDayOfWeek(date: Date = new Date()): string {
        let dateStr = date ? date : new Date()
        let weekNum = dateStr.getDay();
        let week = "";
        switch (weekNum) {
            case 0:
                week = "周天";
                break;
            case 1:
                week = "周一";
                break;
            case 2:
                week = "周二";
                break;
            case 3:
                week = "周三";
                break;
            case 4:
                week = "周四";
                break;
            case 5:
                week = "周五";
                break;
            case 6:
                week = "周六";
                break;
        }
        return week;
    }

    /**
 * 获取某个日期当天的开始时间
 */
    static getDayStartAt(date: Date = new Date()): Date {
        date = date ? date : new Date()
        return new Date(this.formatDate(new Date(date.setHours(0, 0, 0, 0)), 'yyyy-MM-dd hh:mm:ss'))
    }

    /**
     * 获取某个日期当天的结束时间
     */
    static getDayEndAt(date: Date = new Date()): Date {
        date = date ? date : new Date()
        return new Date(this.formatDate(new Date(date.setHours(24, 0, 0, 0)), 'yyyy-MM-dd hh:mm:ss'))
    }
    /**
     * 格式化日期
     * @param date 
     * @param fmt 
     * @returns 
     */
    static formatDate(date: Date, fmt: string = 'yyyy-MM-dd HH:mm:ss'): string {
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        }
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                let str = o[k] + ''
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str))
            }
        }
        return fmt
    }

    private static padLeftZero(str: string) {
        return ('00' + str).substr(str.length)
    }
}